<?php
/***************************
 * header frontend for website.
 ***************************/
session_start();
require_once('../config.php'); // config.php在根目录下
require_once( ROOT.'includes/db.php');
require_once( ROOT.'includes/user.php');
if(isset($_POST['login_submit'])){
	$email = $_POST['email'];
	$password = $_POST['password'];
	if(isset($_POST['remember'])){
		$remember = $_POST['remember'];
	}else{
		$remember = 'off';
	}
	if(!login($email, $password, $remember)){
		if(!isset($_SESSION["login_error"])){
			die('$_SESSION["login_error"] not set')	;
		}
		
		//login failed, get error from session
		$login_error = $_SESSION["login_error"];
		unset( $_SESSION["login_error"] );
	}else{
		$login_success = true;
	}
}

if(isLoggedIn()):
	//user logged in, redirect to user home
	header('Location: '.USER_HOME_URL);
else:
	
require_once('header.php');
if($login_success){
	js_redirect("登录成功，正在转至主页", USER_HOME_URL);
}
?>
	
	<div class='form_column'>
    	<div class="message">
    		<?php echo (isset($login_error))?$login_error:'';?>
    	</div>
		<div class='login'>
			<form action="" method="post">
				<p>邮箱:</p> <input class='email' type="text" name="email" /><br />
				<p>密码:</p> <input class='pwd' type="password" name="password" /><br />
                <p>自动登录：</p><input type="checkbox" name="remember" /><br />
				<input type="submit" name="login_submit" value="登陆Kala" />
			</form>
		</div>
		<?php
			if(isset($_POST['register_submit'])){
				$email = strip_tags($_POST['email']);
				$password = strip_tags($_POST['password']);
				$password_confirm = strip_tags($_POST['password_confirm']);
				$date = date("Y-m-d");
				register($email, $password, $password_confirm);
			}
		?>
		<!--<script type="text/javascript" src="../scripts/user_check.js"></script>-->
		<div class='signup'>
			<form name='form' action="" method="post">
				<p>邮箱: </p><input class='email' type="text" name="email" /> <br />
				<p>密码: </p><input class='pwd' type="password" name="password"/> <br />
				<p>确认密码：</p><input class='pwd' type="password" name="password_confirm"/><br />
                <!-- 验证码 -->
                <img id="captcha" src="../libs/securimage/securimage_show.php" alt="验证码" width="150px" style="border:#999 1px solid" />
				<a href="#" onclick="document.getElementById('captcha').src = '../libs/securimage/securimage_show.php?' + Math.random(); return false"><img src="../libs/securimage/images/refresh.png" width="25px" /></a>
                <br />
                <label for="code">验证码</label>
                <input id="code" type="text" name="captcha_code" size="10" maxlength="6" />
                
                
                <!-- /验证码 -->
                
				<input type="submit" name="register_submit" value="加入Kala" />
			</form>
			<!--
			<div class='ifexist'>
							
						</div>-->
			
		</div>
	</div>
	<div class='intro_column'>
		
	</div>
<?php
endif;
require_once('footer.php');
?>
